home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11079 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: netaxs.com!not-for-mail
  2. From: grulm@netaxs.com (J. A. McNamara)
  3. Newsgroups: comp.lang.c
  4. Subject: binary tree question
  5. Date: 21 Mar 1996 21:11:15 GMT
  6. Organization: Philadelphia's Complete Internet Provider
  7. Message-ID: <4isglj$cgg@netaxs.com>
  8. NNTP-Posting-Host: unix3.netaxs.com
  9. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  10.  
  11. I'm willing to bet that the answer to this is obvious, but I'm trying to 
  12. free each node of a binary tree as it is read -- preferably *after* it's
  13. been read :)  the following code doesn't appear to free the memory, but I 
  14. haven't figured out how to prod my debugger into telling me how much memory
  15. is being used, so I'm not so sure . . .  (by the way, it gives me no
  16. problems in any other respect).  (I'd also like to mention that this is 
  17. *not* homework)
  18.  
  19. /***********************************************************************/
  20. /*link is the linked list struct type; tree_n is the tree node struct type*/
  21. /* addlink() works fine, too, and both are prototyped. */
  22.  
  23. link *inorder(tree_n *tn, link *tail, int *i)   /* read tree into list */
  24.     {
  25.         if (tn != NULL) {
  26.             tail = inorder(tn->l, tail, i);
  27.             tail = addlink(tail, tn->f);      /* hand data ptr to list */
  28.             (*i)++;                           /* keep track of total   */
  29.             tail = inorder(tn->r, tail, i);
  30.         }
  31.         if (tn!=NULL) free(tn); tn=NULL;
  32.         return tail;
  33.     }
  34. /**************************************************************************/
  35.  
  36. I also tried the following:
  37.  
  38. free(tn->l); tn->l=NULL;
  39. free(tn->r); tn->r=NULL;
  40.  
  41. . . .  which gave me some serious garbage, though I'm not quite sure why.
  42.  
  43. any help would be appreciated.  thanks in advance.
  44.  
  45. -- 
  46.                             j a mcnamara  aramancm a j
  47.                         grulm@netaxs.com  moc.sxaten@mlurg
  48.